package com.lwj.diao.Utils;

import com.lwj.diao.JavaSourceHttpServer;

import java.io.InputStream;

public class UtilJar {
    public static final String JAR_PATH = JavaSourceHttpServer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    public static final String JAR_DIR = JAR_PATH.substring(0, JAR_PATH.lastIndexOf("/") + 1);

    public static String getJarDir() {
        if (JAR_DIR.indexOf("/bin/") > -1) {
            return JAR_DIR.substring(0, JAR_DIR.length() - 4);
        }
        return JAR_DIR;
    }

    public static InputStream getProFileByPath(String path) {
        InputStream resourceAsStream = JavaSourceHttpServer.class.getClassLoader().getResourceAsStream(path);
        return resourceAsStream;
    }

    public static String getPath()
    {
        String path = JavaSourceHttpServer.class.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        if(System.getProperty("os.name").contains("dows"))
        {
            path = path.substring(1,path.length());
        }
        if(path.contains("jar"))
        {
            path = path.substring(0,path.lastIndexOf("."));
            return path.substring(0,path.lastIndexOf("/"));
        }
        return path.replace("target/classes/", "");
    }
}
